{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is used to initialize the directory structure for the materials directory using an example YAML (or json) file of a Materials Project document."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import yaml\n",
    "import os\n",
    "import shutil\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "example = \"\"\"## Example response in JSON\n",
    "\n",
    "```json\n",
    "%s\n",
    "```\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "def make_dir(d, rootdir):\n",
    "    for k, v in d.items():\n",
    "        if k == \"_id\":\n",
    "            # Ignore object id key, which is useless.\n",
    "            continue\n",
    "        subpath = os.path.join(rootdir, k)\n",
    "        try:\n",
    "            os.makedirs(subpath)\n",
    "        except:\n",
    "            pass\n",
    "        fname = os.path.join(subpath, \"README.md\")\n",
    "        if not os.path.exists(fname):\n",
    "            with open(fname, \"w\") as f:\n",
    "                if v is not None:\n",
    "                    data = example % json.dumps(v, indent=4)\n",
    "                else:\n",
    "                    data = \"\"\n",
    "                f.write(data)\n",
    "        else:\n",
    "            with open(fname) as f:\n",
    "                data = f.read().strip()\n",
    "            data = data.split(\"## Example response in JSON\")[0]\n",
    "            if \"## Example response in JSON\" not in data:\n",
    "                if v is not None:\n",
    "                    data += \"\\n\\n\"\n",
    "                    data += example % json.dumps(v, indent=4)\n",
    "                    with open(fname, \"w\") as f:\n",
    "                        print \"Adding example to %s\" % k\n",
    "                        f.write(data)\n",
    "\n",
    "        if isinstance(v, dict) and \"formula\" not in k:\n",
    "            # Ignore formulas which has material specific keys.\n",
    "            make_dir(v, rootdir=subpath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding example to icsd_ids\n",
      "Adding example to final_energy_per_atom\n",
      "Adding example to initial_structure\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to @module\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to @class\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to sites\n",
      "Adding example to @class\n",
      "Adding example to @module\n",
      "Adding example to elasticity\n",
      "Adding example to elastic_tensor\n",
      "Adding example to G_Voigt_Reuss_Hill\n",
      "Adding example to G_Reuss\n",
      "Adding example to K_Voigt_Reuss_Hill\n",
      "Adding example to K_Reuss\n",
      "Adding example to nsites\n",
      "Adding example to K_Voigt\n",
      "Adding example to K_VRH\n",
      "Adding example to G_VRH\n",
      "Adding example to homogeneous_poisson\n",
      "Adding example to universal_anisotropy\n",
      "Adding example to calculations\n",
      "Adding example to kpoint_density\n",
      "Adding example to energy_cutoff\n",
      "Adding example to pseudopotentials\n",
      "Adding example to poisson_ratio\n",
      "Adding example to G_Voigt\n",
      "Adding example to elastic_anisotropy\n",
      "Adding example to encut\n",
      "Adding example to ntask_ids\n",
      "Adding example to oxide_type\n",
      "Adding example to has_bandstructure\n",
      "Adding example to hubbards\n",
      "Adding example to task_ids\n",
      "Adding example to magnetic_type\n",
      "Adding example to reduced_cell_formula\n",
      "Adding example to cifs\n",
      "Adding example to primitive\n",
      "Adding example to refined\n",
      "Adding example to conventional_standard\n",
      "Adding example to band_gap\n",
      "Adding example to optimize_structure_gap\n",
      "Adding example to band_gap\n",
      "Adding example to is_direct\n",
      "Adding example to search_gap\n",
      "Adding example to band_gap\n",
      "Adding example to is_direct\n",
      "Adding example to density\n",
      "Adding example to xrd\n",
      "Adding example to Co\n",
      "Adding example to pattern\n",
      "Adding example to created_at\n",
      "Adding example to meta\n",
      "Adding example to updated_at\n",
      "Adding example to wavelength\n",
      "Adding example to in_angstroms\n",
      "Adding example to element\n",
      "Adding example to Fe\n",
      "Adding example to pattern\n",
      "Adding example to created_at\n",
      "Adding example to meta\n",
      "Adding example to updated_at\n",
      "Adding example to wavelength\n",
      "Adding example to in_angstroms\n",
      "Adding example to element\n",
      "Adding example to Ag\n",
      "Adding example to pattern\n",
      "Adding example to created_at\n",
      "Adding example to meta\n",
      "Adding example to updated_at\n",
      "Adding example to wavelength\n",
      "Adding example to in_angstroms\n",
      "Adding example to element\n",
      "Adding example to Cr\n",
      "Adding example to pattern\n",
      "Adding example to created_at\n",
      "Adding example to meta\n",
      "Adding example to updated_at\n",
      "Adding example to wavelength\n",
      "Adding example to in_angstroms\n",
      "Adding example to element\n",
      "Adding example to Mo\n",
      "Adding example to pattern\n",
      "Adding example to created_at\n",
      "Adding example to meta\n",
      "Adding example to updated_at\n",
      "Adding example to wavelength\n",
      "Adding example to in_angstroms\n",
      "Adding example to element\n",
      "Adding example to Cu\n",
      "Adding example to pattern\n",
      "Adding example to created_at\n",
      "Adding example to meta\n",
      "Adding example to updated_at\n",
      "Adding example to wavelength\n",
      "Adding example to in_angstroms\n",
      "Adding example to element\n",
      "Adding example to delta_volume\n",
      "Adding example to nsites\n",
      "Adding example to chemsys\n",
      "Adding example to efermi\n",
      "Adding example to original_task_id\n",
      "Adding example to bv_structure\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to @module\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to @class\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to sites\n",
      "Adding example to @class\n",
      "Adding example to @module\n",
      "Adding example to icsd_id\n",
      "Adding example to anonymous_formula\n",
      "Adding example to blessed_tasks\n",
      "Adding example to GGA optimize structure (2x)\n",
      "Adding example to run_type\n",
      "Adding example to elements\n",
      "Adding example to ionic_steps\n",
      "Adding example to warnings\n",
      "Adding example to pretty_formula\n",
      "Adding example to snl\n",
      "Adding example to elements\n",
      "Adding example to is_ordered\n",
      "Adding example to anonymized_formula\n",
      "Adding example to snl_id\n",
      "Adding example to @module\n",
      "Adding example to chemsystem\n",
      "Adding example to snl_timestamp\n",
      "Adding example to composition_dict\n",
      "Adding example to Al\n",
      "Adding example to Pt\n",
      "Adding example to @class\n",
      "Adding example to about\n",
      "Adding example to created_at\n",
      "Adding example to $date\n",
      "Adding example to _icsd\n",
      "Adding example to icsd_id\n",
      "Adding example to comments\n",
      "Adding example to _materialsproject\n",
      "Adding example to deprecated\n",
      "Adding example to crystal_deprecated\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to volume\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to alpha\n",
      "Adding example to beta\n",
      "Adding example to gamma\n",
      "Adding example to space_group\n",
      "Adding example to hermann_mauguin\n",
      "Adding example to centering\n",
      "Adding example to crystal_system\n",
      "Adding example to number\n",
      "Adding example to point_group\n",
      "Adding example to is_standard\n",
      "Adding example to schoenflies\n",
      "Adding example to laue\n",
      "Adding example to cctbx_name\n",
      "Adding example to hall\n",
      "Adding example to db\n",
      "Adding example to sites\n",
      "Adding example to transforms\n",
      "Adding example to original_structure_order\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to @module\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to @class\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to sites\n",
      "Adding example to @class\n",
      "Adding example to @module\n",
      "Adding example to mps_ids\n",
      "Adding example to crystal_id_deprecated\n",
      "Adding example to snl_id\n",
      "Adding example to spacegroup\n",
      "Adding example to lattice_type\n",
      "Adding example to symbol\n",
      "Adding example to crystal_system\n",
      "Adding example to point_group\n",
      "Adding example to hall\n",
      "Adding example to number\n",
      "Adding example to references\n",
      "Adding example to authors\n",
      "Adding example to remarks\n",
      "Adding example to projects\n",
      "Adding example to history\n",
      "Adding example to reduced_cell_formula_abc\n",
      "Adding example to snlgroup_key\n",
      "Adding example to nelements\n",
      "Adding example to reduced_cell_formula\n",
      "Adding example to nsites\n",
      "Adding example to sites\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to @module\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to @class\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to is_valid\n",
      "Adding example to formula\n",
      "Adding example to decomposes_to\n",
      "Adding example to cpu_time\n",
      "Adding example to updated_at\n",
      "Adding example to $date\n",
      "Adding example to volume\n",
      "Adding example to nkpts\n",
      "Adding example to total_magnetization\n",
      "Adding example to is_hubbard\n",
      "Adding example to unit_cell_formula\n",
      "Adding example to structure\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to sites\n",
      "Adding example to @class\n",
      "Adding example to @module\n",
      "Adding example to cif\n",
      "Adding example to formation_energy_per_atom\n",
      "Adding example to snl_final\n",
      "Adding example to elements\n",
      "Adding example to is_ordered\n",
      "Adding example to anonymized_formula\n",
      "Adding example to snl_id\n",
      "Adding example to @module\n",
      "Adding example to chemsystem\n",
      "Adding example to @class\n",
      "Adding example to about\n",
      "Adding example to created_at\n",
      "Adding example to $date\n",
      "Adding example to _icsd\n",
      "Adding example to icsd_id\n",
      "Adding example to comments\n",
      "Adding example to _materialsproject\n",
      "Adding example to deprecated\n",
      "Adding example to crystal_deprecated\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to volume\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to alpha\n",
      "Adding example to beta\n",
      "Adding example to gamma\n",
      "Adding example to space_group\n",
      "Adding example to hermann_mauguin\n",
      "Adding example to centering\n",
      "Adding example to crystal_system\n",
      "Adding example to number\n",
      "Adding example to point_group\n",
      "Adding example to is_standard\n",
      "Adding example to schoenflies\n",
      "Adding example to laue\n",
      "Adding example to cctbx_name\n",
      "Adding example to hall\n",
      "Adding example to db\n",
      "Adding example to sites\n",
      "Adding example to transforms\n",
      "Adding example to original_structure_order\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to @module\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to @class\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to sites\n",
      "Adding example to @class\n",
      "Adding example to @module\n",
      "Adding example to mps_ids\n",
      "Adding example to crystal_id_deprecated\n",
      "Adding example to snl_id\n",
      "Adding example to spacegroup\n",
      "Adding example to lattice_type\n",
      "Adding example to symbol\n",
      "Adding example to crystal_system\n",
      "Adding example to point_group\n",
      "Adding example to hall\n",
      "Adding example to number\n",
      "Adding example to references\n",
      "Adding example to authors\n",
      "Adding example to remarks\n",
      "Adding example to projects\n",
      "Adding example to history\n",
      "Adding example to reduced_cell_formula_abc\n",
      "Adding example to snlgroup_key\n",
      "Adding example to nelements\n",
      "Adding example to reduced_cell_formula\n",
      "Adding example to nsites\n",
      "Adding example to sites\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to @module\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to @class\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to is_valid\n",
      "Adding example to formula\n",
      "Adding example to final_energy\n",
      "Adding example to input\n",
      "Adding example to crystal\n",
      "Adding example to lattice\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to @module\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to @class\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to sites\n",
      "Adding example to @class\n",
      "Adding example to @module\n",
      "Adding example to lattice_rec\n",
      "Adding example to a\n",
      "Adding example to c\n",
      "Adding example to b\n",
      "Adding example to matrix\n",
      "Adding example to @module\n",
      "Adding example to volume\n",
      "Adding example to beta\n",
      "Adding example to @class\n",
      "Adding example to alpha\n",
      "Adding example to gamma\n",
      "Adding example to parameters\n",
      "Adding example to NELMDL\n",
      "Adding example to LMUSIC\n",
      "Adding example to WC\n",
      "Adding example to LCHIMAG\n",
      "Adding example to INIWAV\n",
      "Adding example to APACO\n",
      "Adding example to IBRION\n",
      "Adding example to ICORELEVEL\n",
      "Adding example to MODEL_GW\n",
      "Adding example to LSCAAWARE\n",
      "Adding example to LDIPOL\n",
      "Adding example to ENCUTFOCK\n",
      "Adding example to LRPA\n",
      "Adding example to HFSCREEN\n",
      "Adding example to MAXMIX\n",
      "Adding example to EMAX\n",
      "Adding example to LBERRY\n",
      "Adding example to ENAUG\n",
      "Adding example to LHFONE\n",
      "Adding example to ISYM\n",
      "Adding example to MODEL_ALPHA\n",
      "Adding example to AEXX\n",
      "Adding example to LTCTE\n",
      "Adding example to IDIOT\n",
      "Adding example to LELF\n",
      "Adding example to HFALPHA\n",
      "Adding example to NBANDS\n",
      "Adding example to LMAXPAW\n",
      "Adding example to LSPIRAL\n",
      "Adding example to ISMEAR\n",
      "Adding example to ENCUTGWSOFT\n",
      "Adding example to EBREAK\n",
      "Adding example to IWAVPR\n",
      "Adding example to WEIMIN\n",
      "Adding example to NEDOS\n",
      "Adding example to LCOMPAT\n",
      "Adding example to FOURORBIT\n",
      "Adding example to LVEL\n",
      "Adding example to LDAU\n",
      "Adding example to LMAXMIX\n",
      "Adding example to NOMEGAR\n",
      "Adding example to ODDONLYGW\n",
      "Adding example to ISPIN\n",
      "Adding example to GGA_COMPAT\n",
      "Adding example to BMIX_MAG\n",
      "Adding example to NMAXFOCKAE\n",
      "Adding example to SYMPREC\n",
      "Adding example to LVTOT\n",
      "Adding example to TEEND\n",
      "Adding example to AVECCONST\n",
      "Adding example to MAGDIPOL\n",
      "Adding example to ODDONLY\n",
      "Adding example to ORBITALMAG\n",
      "Adding example to EDIFFG\n",
      "Adding example to LZEROZ\n",
      "Adding example to NGX\n",
      "Adding example to NGY\n",
      "Adding example to LASYNC\n",
      "Adding example to ROPT\n",
      "Adding example to NBLOCK\n",
      "Adding example to LMAGBLOCH\n",
      "Adding example to NRMM\n",
      "Adding example to NBANDSLF\n",
      "Adding example to IDIPOL\n",
      "Adding example to HFSCREENC\n",
      "Adding example to EVENONLYGW\n",
      "Adding example to LRHFCALC\n",
      "Adding example to EFERMI\n",
      "Adding example to LREAL_COMPAT\n",
      "Adding example to OMEGATL\n",
      "Adding example to EDIFF\n",
      "Adding example to LEPSILON\n",
      "Adding example to EFIELD\n",
      "Adding example to DIPOL\n",
      "Adding example to MAGMOM\n",
      "Adding example to LSORBIT\n",
      "Adding example to NUPDOWN\n",
      "Adding example to NELMIN\n",
      "Adding example to LTHOMAS\n",
      "Adding example to LSCALAPACK\n",
      "Adding example to NBLK\n",
      "Adding example to NFREE\n",
      "Adding example to ENCUT4O\n",
      "Adding example to EVENONLY\n",
      "Adding example to NELECT\n",
      "Adding example to LMAXMP2\n",
      "Adding example to LCORR\n",
      "Adding example to EXXOEP\n",
      "Adding example to AMIX\n",
      "Adding example to TIME\n",
      "Adding example to LORBIT\n",
      "Adding example to ICHARG\n",
      "Adding example to ADDGRID\n",
      "Adding example to QSPIRAL\n",
      "Adding example to AGGAX\n",
      "Adding example to NKREDLFX\n",
      "Adding example to NKREDLFY\n",
      "Adding example to NKREDLFZ\n",
      "Adding example to IALGO\n",
      "Adding example to NELM\n",
      "Adding example to LPARD\n",
      "Adding example to PSTRESS\n",
      "Adding example to LMODELHF\n",
      "Adding example to CSHIFT\n",
      "Adding example to NGYF\n",
      "Adding example to DEPER\n",
      "Adding example to LWAVE\n",
      "Adding example to SELFENERGY\n",
      "Adding example to NGZ\n",
      "Adding example to NPACO\n",
      "Adding example to LASPH\n",
      "Adding example to LSPECTRAL\n",
      "Adding example to ENCUTLF\n",
      "Adding example to EMIN\n",
      "Adding example to LSCALU\n",
      "Adding example to KINTER\n",
      "Adding example to NSIM\n",
      "Adding example to ISIF\n",
      "Adding example to MODEL_EPS0\n",
      "Adding example to I_CONSTRAINED_M\n",
      "Adding example to GGA\n",
      "Adding example to TEBEG\n",
      "Adding example to NGXF\n",
      "Adding example to LTETE\n",
      "Adding example to NBANDSGW\n",
      "Adding example to MREMOVE\n",
      "Adding example to INIMIX\n",
      "Adding example to AMIN\n",
      "Adding example to NGZF\n",
      "Adding example to LMETAGGA\n",
      "Adding example to NPAR\n",
      "Adding example to LUSEW\n",
      "Adding example to LGWLF\n",
      "Adding example to MIXPRE\n",
      "Adding example to MAXMEM\n",
      "Adding example to ALDAC\n",
      "Adding example to L2ORDER\n",
      "Adding example to OMEGAGRID\n",
      "Adding example to PREC\n",
      "Adding example to NWRITE\n",
      "Adding example to ALDAX\n",
      "Adding example to POMASS\n",
      "Adding example to LHFCALC\n",
      "Adding example to SHIFTRED\n",
      "Adding example to DIM\n",
      "Adding example to LGAUGE\n",
      "Adding example to NKREDX\n",
      "Adding example to NKREDY\n",
      "Adding example to NKREDZ\n",
      "Adding example to VOSKOWN\n",
      "Adding example to LNABLA\n",
      "Adding example to SMASS\n",
      "Adding example to ENMAX\n",
      "Adding example to AMIX_MAG\n",
      "Adding example to RWIGS\n",
      "Adding example to BMIX\n",
      "Adding example to ISTART\n",
      "Adding example to AGGAC\n",
      "Adding example to SIGMA\n",
      "Adding example to LDIAG\n",
      "Adding example to LMONO\n",
      "Adding example to SAXIS\n",
      "Adding example to LOPTICS\n",
      "Adding example to POTIM\n",
      "Adding example to LMAXFOCK\n",
      "Adding example to NSW\n",
      "Adding example to ENCUTGW\n",
      "Adding example to STM\n",
      "Adding example to NOMEGA\n",
      "Adding example to SCISSOR\n",
      "Adding example to LPLANE\n",
      "Adding example to LREAL\n",
      "Adding example to KBLOCK\n",
      "Adding example to TELESCOPE\n",
      "Adding example to MAGATOM\n",
      "Adding example to LCHARG\n",
      "Adding example to EPSILON\n",
      "Adding example to LNONCOLLINEAR\n",
      "Adding example to OMEGAMAX\n",
      "Adding example to SYSTEM\n",
      "Adding example to IMIX\n",
      "Adding example to ENINI\n",
      "Adding example to ANTIRES\n",
      "Adding example to incar\n",
      "Adding example to NELM\n",
      "Adding example to ISPIN\n",
      "Adding example to PREC\n",
      "Adding example to IBRION\n",
      "Adding example to LREAL\n",
      "Adding example to LCALCEPS\n",
      "Adding example to ISMEAR"
     ]
    }
   ],
   "source": [
    "for fname in [\"mp-188.yaml\", \"mp-1234.yaml\"]:\n",
    "    with open(fname) as f:\n",
    "        doc = yaml.load(f)\n",
    "    make_dir(doc, rootdir=\"../materials\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
